<?php
defined('THINK_PATH') or exit();
// +----------------------------------------------------------------------------
// | PHP@妖孽 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * PHP开发基础框架home函数库
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-8-8
 +------------------------------------------------------------------------------
 */
/**
 * 检测用户是否登录
 * @return integer 0-未登录，大于0-当前登录用户ID
 * @author PHP@妖孽  <dino_ma@163.com>
 */
function is_login(){
    $user = session('customer_auth');
    if (empty($user)) {
        return 0;
    } else {
        return session('customer_auth_sign') == data_auth_sign($user) ? $user['customer_id'] : 0;
    }
}

/**
 * 根据商品id随机生成一个订单数量
 * @param int $goods_stock 商品库存
 * @return int
 * @author fzh
 */
function randGoodsOrderCount($goods_stock){
    $goods_stock = (int)$goods_stock;
    if($goods_stock == 0){
        return 0;    
    }
    
    $count = strlen($goods_stock);
    if($count == 1){
        return $goods_stock;
    }
    
    $num = substr($goods_stock,-1,1);
    if($num > 0){
        return $num;
    }else{
        return 10;
    }
}

/**
 * 根据商品id随机生成一个关注数量
 * @param int $goods_id 商品id
 * @return int
 * @author fzh
 */
function randGoodsFollowCount($goods_id){
    return $goods_id%100+(int)((NOW_TIME-1448275688)/1800);
}

/**
 * @todo  生成二维码
 * @param string $text 生成内容
 * @return img 图片
 * @author fzh<fanzhaohai@hrbjjhh.com>
 */
function getQrCode($text, $outfile=false, $level=QR_ECLEVEL_L, $size=8, $margin=4, $saveandprint=false){
    import('Common.Msj.phpqrcode','','.php');
    $enc = \QRencode::factory($level, $size, $margin);
    return $enc->encodePNG($text, $outfile, $saveandprint=false);
}